import math
import random
import numpy
import impart.entity
import impart.data_input
import impart.random_num
import re

#执行ziwei操作，传入player，传出长度变化，

#根据tagID获取tag名称和介绍
#第一个返回名称，第二个返回介绍
def get_tag_name(ID):
    for record in impart.data_input.Tag:
        if record[0] == ID:
            #print(record[1])
            return record[1], record[2]



#升级检测,ziwei/suo/pk后判断
#根据一个阶梯函数，判断每一级所需的升级经验，到达后经验清零，等级加一
#当前版本每一级升级经验要求为 100+当前等级*10
#传入exp，level，传出等级，经验和文字
def check_upgrade(EXP,Level):
    upgrade_text = ""
    #满足条件可以升级
    if EXP>=100+(Level*10):
        Level = Level+1
        EXP = EXP - (100+(Level*10))
        upgrade_text = f"{upgrade_text}恭喜你升级了，当前等级{Level}"#后续在这儿增加角色属性
        return Level,EXP,upgrade_text #返回元组
    return Level, EXP, upgrade_text  # 返回元组

#生成玩家信息
#初始化玩家信息
#当前版本拥有信息为：
#力量/Strong,智力/Intelligence，精力值/Tili，天赋值/Talent，经验/EXP，等级/Level，处世之道/Way,文化程度/Education，精神力/San,财富值/Money，魅力/charm
#长度/Length,今日注入/TodayInjection,总注入/TatallInjection,今日fun/TodayFun,总Fun/TatalFun
#Tag(10001开始)
def Init_player(qq,qqgroup):
    player = impart.entity.Player() #初始化结构体
    player.QQID = qq
    player.EffectGroup = qqgroup
    player.Strong = random.randint(2,10)#力量2-10
    player.Intelligence = random.randint(1,7)+(9-player.Strong)#为了平衡，力量越高，智力相应变低.智力在力量为6点时为7点
    player.Tili = 180#体力上限180
    player.Talent = random.uniform(0.8,1.2)#升级倍率
    player.EXP = 0
    player.Level = 1
    player.Way = random.randint(0,100)#刚正、仁善、中庸、叛逆、唯我
    player.Education = 1
    player.San = 60 #san初始60
    player.Money = random.randint(0,100)
    player.Charm = 0
    player.Length = round(random.randint(50, 150) / 10, 2)#长度5-15cm
    player.TodayInjection = 0
    player.TatallInjection = 0
    player.TodayFun = 0
    player.TatalFun = 0
    player.Tag = [10000,]
    player.Tag.append(impart.data_input.InitTag[random.randint(0, len(impart.data_input.InitTag) - 1)][0])
    player.Weapon = [200000,]
    #player.Weapon.append(200001)
    return player
    #返回的是一个地址,使用时需要.出来。以及日后需要增加属性，只需要去类里面增加即可

#执行随机事件
#1.先传入玩家player，然后进行扫描所有可触发的所有事件，
#2.检测是否有必须下一次触发的事件，比如（中毒后，会增加中毒tag），下一次触发就会增加解毒事件（必须触发），
#3.必须触发事件如果有多个。可以用1，2，3，4来表明优先级，数字越大优先级越高。如果相同，比如，捡到药丸，必须触发吃下红和吃下蓝。那么这两个都是1，会随机取出一个触发。
#4.最后把触发的事件,player现在的状态传出

#核对输入的tag数组是否完全吻合玩家的tag列表
#输入需要核对的list和玩家信息
def TAGS(list,TAG):
    result = numpy.isin(list,TAG).all()
    #print(result)
    return result
def get_fourth_col(row):
    return row[4]
def Event(player):
    #取出玩家数据
    QQI=player.QQID
    EFF=player.EffectGroup
    STR=player.Strong
    INT=player.Intelligence
    TIL=player.Tili
    TAL=player.Talent
    EXP=player.EXP
    LEV=player.Level
    WAY=player.Way
    EDU=player.Education
    SAN=player.San
    MON=player.Money
    CHA=player.Charm
    LEN=player.Length
    TOI=player.TodayInjection
    TAI=player.TatallInjection
    TOF=player.TodayFun
    TAF=player.TatalFun
    TAG=player.Tag
    WEA=player.Weapon

    #能够执行的事件放入一个列表里
    CanEvent=[]
    for i in range(len(impart.data_input.Events)):
        #读取每一个触发条件
        s = str(impart.data_input.Events[i][3])
        if re.search(r'\]\)$', s):
            s = impart.data_input.Events[i][3][:-1]+",TAG)"
        if s != 'nan':
            if eval(s):#满足条件，加入CanEvent数组
                CanEvent.append(impart.data_input.Events[i])
        if s == 'nan':#如果为空，也加入数组
            CanEvent.append(impart.data_input.Events[i])
    #print(CanEvent)

    # 找到优先级最大的事件群
    max_priority = max(event[4] for event in CanEvent)
    max_priority_events = [event for event in CanEvent if event[4] == max_priority]

    # 从最大优先级事件中随机选择一个事件
    selected_event = random.choice(max_priority_events)

    #读取出event
    event=selected_event[0]
    description=selected_event[2]
    #执行此事件的数值改变赋值
    code = str(selected_event[5])
    #print(code)
    if code != 'nan':
        dictionary_result={'STR':STR,'INT':INT,'TIL':TIL,'TAL':TAL,'EXP':EXP,'LEV':LEV,'WAY':WAY,'EDU':EDU,'SAN':SAN,'MON':MON,'CHA':CHA,'LEN':LEN,'TOI':TOI,'TAI':TAI,'TOF':TOF,'TAF':TAF}
        statements = code.split("|")
        for statement in statements:
            exec (statement,dictionary_result)
        if 'STR' in dictionary_result:
            STR = dictionary_result["STR"]
        if 'INT' in dictionary_result:
            INT = dictionary_result["INT"]
        if 'TIL' in dictionary_result:
            TIL = dictionary_result["TIL"]
        if 'TAL' in dictionary_result:
            TAL = dictionary_result["TAL"]
        if 'EXP' in dictionary_result:
            EXP = dictionary_result["EXP"]
        if 'LEV' in dictionary_result:
            LEV = dictionary_result["LEV"]
        if 'WAY' in dictionary_result:
            WAY = dictionary_result["WAY"]
        if 'EDU' in dictionary_result:
            EDU = dictionary_result["EDU"]
        if 'SAN' in dictionary_result:
            SAN = dictionary_result["SAN"]
        if 'MON' in dictionary_result:
            MON = dictionary_result["MON"]
        if 'CHA' in dictionary_result:
            CHA = dictionary_result["CHA"]
        if 'LEN' in dictionary_result:
            LEN = dictionary_result["LEN"]
        if 'TOI' in dictionary_result:
            TOI = dictionary_result["TOI"]
        if 'TAI' in dictionary_result:
            TAI = dictionary_result["TAI"]
        if 'TOF' in dictionary_result:
            TOF = dictionary_result["TOF"]
        if 'TAF' in dictionary_result:
            TAF = dictionary_result["TAF"]
    #执行此事件的tag增加
    #tag_add = []
    tag_text = ""
    addtag = str(selected_event[6])
    if addtag != 'nan':
        statements = addtag.split("|")
        for statement in statements:
            statement = int(float(statement))
            if (statement in TAG) == False:
                tagname,tagdic = get_tag_name(statement)
                if str(tagdic) != 'nan':#如果tag没有描述，则不增加后面的话，作为一个事件触发器
                    tag_text = tag_text + f"【{tagname}】:{tagdic}"
                TAG.append(statement)
                #tag_add.append(statement)
    #执行此事件的tag删除
    refusetag = str(selected_event[7])
    if refusetag != 'nan':
        statements = refusetag.split("|")
        for statement in statements:
            statement = int(float(statement))
            if statement in TAG:
                TAG.remove(statement)#只有存在的时候才删除
    # 执行此事件的武器增加
    addtweapon = str(selected_event[8])
    if addtweapon != 'nan':
        statements = addtweapon.split("|")
        for statement in statements:
            statement = int(float(statement))
            if (statement in WEA) == False:
                WEA.append(statement)
    # 执行此事件的武器删除
    refuseweapon = str(selected_event[9])
    if refuseweapon != 'nan':
        statements = refuseweapon.split("|")
        for statement in statements:
            statement = int(float(statement))
            if statement in WEA:
                WEA.remove(statement)  # 只有存在的时候才删除
    #整合整个player数据
    player.Strong = STR
    player.Intelligence = INT
    player.Tili = TIL
    player.Talent = TAL
    player.EXP = EXP
    player.Level = LEV
    player.Way = WAY
    player.Education = EDU
    player.San = SAN
    player.Money = MON
    player.Charm = CHA
    player.Length = LEN
    player.TodayInjection = TOI
    player.TatallInjection = TAI
    player.TodayFun = TOF
    player.TatalFun = TAF
    player.Tag = TAG
    player.Weapon = WEA
    #最后返回以下4个作为元组
    return event,description,tag_text,player

#传入player，返回一段文本和player
def ziwei(player):
    EXPADD = impart.random_num.random_EXPADD()
    lengthadd = impart.random_num.random_Length()
    player.Length = player.Length + lengthadd
    player.TatalFun = player.TatalFun + 1
    player.TodayFun = player.TodayFun + 1
    player.Tili = player.Tili - 10
    player.EXP = player.EXP + EXPADD
    ziwei_text = f"【{player.QQID}】疯狂求导，渐入佳境，牛牛长度增加{round(lengthadd,2)}cm,长达{round(player.Length,2)}cm。"
    return ziwei_text,player

#传入两个player，传出一段文字和两个player
def suo(playera,playerb):
    InjectionADD = impart.random_num.random_Injection()  # 自己被注入增加量
    EXPADD = impart.random_num.random_EXPADD()  # 双方增加经验
    lengthadd = impart.random_num.random_Length() #对方增加长度
    playera.TodayInjection = playera.TodayInjection + InjectionADD
    playera.TodayFun = playera.TodayFun + 1
    playera.TatalFun = playera.TatalFun + 1
    playerb.Length = playerb.Length +lengthadd
    playerb.TodayFun = playera.TodayFun + 1
    playerb.TatalFun = playera.TatalFun + 1
    suo_text = f"【{playera.QQID}】兽性大发开始拼命嗦【{playerb.QQID}】的牛牛，自己被注入{round(InjectionADD,2)}ml,对方牛牛增长{round(lengthadd,2)}cm。"
    return suo_text,playera,playerb

#判断是否能触发事件,现在是百分之90的几率触发事件
def isEvent():
    if random.randint(0,10)>1:
        return True
    else:
        return False

